import numpy as np
import cv2
from PIL import Image
from math import pi, exp, sqrt
import numpy
import rot

#pemite o print de tuda a maytrix
numpy.set_printoptions(threshold=numpy.nan)

def conv_s(image1, image2):
	soma = 0
	
	for i in range(0, image1.shape[0]):
		for j in range(0, image1.shape[1]):
			soma = soma + ((image1[i][j])* (image2[i][j]))
	return soma
#--------------------------------------------------------------------
def mascara_k(im):

	image2 = np.zeros(im.shape)

	m0 = np.array([[-3,-3,5],[-3,0,5],[-3,-3,5]])
	m1 = np.array([[-3,5,5],[-3,0,5],[-3,-3,-3]])
	m2 = np.array([[5,5,5],[-3,0,-3],[-3,-3,-3]])
	m3 = np.array([[5,5,-3],[5,0,-3],[-3,-3,-3]])
	m4 = np.array([[5,-3,-3],[5,0,-3],[5,-3,-3]])
	m5 = np.array([[-3,-3,-3],[5,0,-3],[5,5,-3]])
	m6 = np.array([[-3,-3,-3],[-3,0,-3],[5,5,5]])
	m7 = np.array([[-3,-3,-3],[-3,0,5],[-3,5,5]])
	m = [m0,m1,m2,m3,m4,m5,m6,m7]
	direct = {'m0': '000', 'm1': '001', 'm2': '010', 'm3': '011', 'm4': '100', 'm5': '101','m6': '110', 'm7': '111'}
		
	for i in range(1, im.shape[0]-1):
		for j in range(1, im.shape[1]-1):
			countmaxi = 0
			countmini = 0
			maxi = 0
			mini = 0
			cont = 0
			for t in m:
				res=0
				ima = im[i-1:i+2,j-1:j+2]

				res = conv_s(t,ima)

				if (res >= maxi):
					countmaxi = cont
				if (res < mini):
					countmini = cont
				cont=cont +1
			dire = str ('00') + str(direct['m'+str(countmaxi)+'']) + str(direct['m'+str(countmini) + '']	)
			a =  int(dire, 2)
			image2[i-1][j-1] = a
	
	cv2.imwrite("auxk.pgm",image2)	
	return image2
#------------------------------------------------------------------

def f_gauss(tam,sigma):
	gauss = np.zeros((tam,tam))
	r= tam-((tam/2)+1)
	m1 = (1.0/(2*3.14*sigma**2))
	
	for i in range(tam-(tam+r),tam-(tam-r)+1):
		for j in range(tam-(tam+r),tam-(tam-r)+1):
			m2 = -(i**2+j**2)/(2.0*sigma**2)
			gauss[i+1][j+1] =(m1*exp(m2))#*16   #****
	return gauss
#------------------------------------------------------------------------

def d_gauss(tam,sigma,k):
	r= tam-((tam/2)+1)
	gauss = np.zeros((tam,tam))
	for i in range(tam-(tam+r), tam-(tam-r)+1):
		for j in range(tam-(tam+r), tam-(tam-r)+1):
			#m1 = (sqrt((((i+k)**2+j**2)/(2.0*sigma**2))/(2*pi*sigma**4)))  #falta o -
			m1 = ((i+k)/(2*pi*sigma**4))  #falta o -
			m2 = -((i+k)**2+j**2)/(2.0*sigma**2)
			gauss[i+1][j+1] =(m1*exp(m2))*100    #******
	return gauss
#---------------------------------------------------------------------------

def m_gauss(tam,dg,g):
	gauss = np.zeros((tam,tam))
	for i in range(0, tam):
		for j in range(0, tam):
			gauss[i][j]= (dg[i][j]) * (g[i][j])
	return gauss
#-------------------------------------------------------------------------

def mascara_g(im, mg):
	
	image2 = np.zeros(im.shape)

	m0 = mg
	m1 = np.zeros((3,3))
	m2 = np.zeros((3,3))
	m3 = np.zeros((3,3))
	m4 = np.zeros((3,3))
	m5 = np.zeros((3,3))
	m6 = np.zeros((3,3))
	m7 = np.zeros((3,3))

	m = [m0,m1,m2,m3,m4,m5,m6,m7]
	direct = {'m0': '000', 'm1': '001', 'm2': '010', 'm3': '011', 'm4': '100', 'm5': '101','m6': '110', 'm7': '111'}
	
	for i in range(1, im.shape[0]-1):
		for j in range(1, im.shape[1]-1):
			countmaxi = 0
			countmini = 0
			maxi = 0
			mini = 0
			cont = 0
			t=m0
			for aa in range(0,8):
				res=0
				ima = im[i-1:i+2,j-1:j+2]
				res = conv_s(t,ima)
				if (res >= maxi):
					countmaxi = cont
				if (res < mini):
					countmini = cont
				cont=cont +1
				t = rot.rot45(t)
			#dire = str ('00') + str(direct['m'+str(countmaxi)+'']) + str(direct['m'+str(countmini) + '']	)
			#a =  int(dire, 2)
			image2[i-1][j-1] = res#a
	cv2.imwrite("auxg.pgm",image2)
	return image2
#---------------------------------------------------------------------------------

def crop (im1):
	im = Image.open(im1)
	imwith = im.size[0]
	imheigh = im.size[1]
	qua = (imwith/200)		#Tamanho a partir do qual se divide a imagem 
	
	if (qua < 2) :
		im.save("ima"+str(0)+str(0)+".pgm")
		return 1
	else:
		stepw = (imwith/qua)
		steph = (imheigh/qua)
	
		for i in range (0,qua+2):
			for j in range (0,qua+2):
				box = ((i)*stepw,(j)*steph,(i+1)*stepw, (j+1)*steph)
				region = im.crop(box)
				region.save ("ima"+str(i)+str(j)+".pgm")
		return qua+1
#------------------------------------------------------------------------

def matrizar (im):

	ini=0	
	fim =0
	c = False
	f = False
	valor =0 
	lista = []
	for t in range(1,len(im)):
		try:	
			int(im[t])
			if (c == False):			
				ini= t
				c = True
		except:
			if (c == True):
				f = True
				fim = t
				
		if (f == True):		
			c=False
			f = False
			offset = fim - ini	
			fim = 0
			ini=0	
			if offset == 1:
				valor = im[t-1]
			if offset == 2:
				valor = str(im[t-2])+str(im[t-1])
			if offset == 3:
				valor = str(im[t-3])+str(im[t-2])+str(im[t-1])
			if offset == 4:
				valor = str(im[t-4])+str(im[t-3])+str(im[t-2])+str(im[t-1])	
			lista.append(valor)
	lista = map(int,lista)
	lista = np.array(lista)
	return lista
#-------------------------------------------------------------------------------

def xquadrado (vetorbd, vetorprova):
	comp = 0
	 
	if vetorbd.shape <> vetorprova.shape:
		print 'Vetores diferentes'
		exit (0)

	for n in range(0,vetorbd.shape[0]):
		comp += ((vetorbd[n]-vetorprova[n])**2)/(vetorbd[n]+vetorprova[n])

	return comp
#--------------------------------------------------------------------------------
